package com.scenic.rd_android.testhandler;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.view.View;
import android.widget.TextView;

import com.nova.scenic.projectlibs.util.BaseActivity;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

/**
 * Created by scenic on 16/6/22.
 * 总结: Handle 是需要一个 Looper的,如果在UI线程创建,那么 就是拥有UI线程的Looper,通过sendmessage 发送给 looper里面的 messagequeue,然后messagequeue里面的消息在分发,这个过程就是在UI线程执行的
 *
 */
public class TestHandlerActivity extends BaseActivity {

    private MyHandler handler = null;


    static MyLog myLog = new MyLog("TestHandlerActivity");

    static {
        myLog.setPattern("  - %m");
    }

    private HandlerThread handlerThread;

    private TextView mMessage;
    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_handler);
        mMessage = (TextView) findViewById(R.id.id_textView_message);

        handlerThread = new HandlerThread("test_handler");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();

        handler = new MyHandler(looper, null);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(0);

        handler.getLooper().getQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                myLog.info("message queue idle");
                return false;
            }
        });

//        MyThread thread = new MyThread();
//        thread.start();
//
//        handler = new TimerHandler(thread.myLooper(), null);
//        handler.sendEmptyMessage(0);
//        myLog.info("thread alive " + thread.isAlive());
    }

    @TargetApi(Build.VERSION_CODES.M)
    public void showMessageQueueInfo(View view) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("isIdel " + handler.getLooper().getQueue().isIdle()).append("\n");
        stringBuffer.append("isIdel " + handler.getLooper().getQueue()).append("\n");

        mMessage.setText("size is " + handler.getLooper().getQueue().isIdle());
    }


    private class MyThread extends Thread {
        @Override
        public void run() {
            Looper.prepare();
            Looper.loop();

        }

        public Looper myLooper() {
            return Looper.myLooper();
        }
    }

    private class MyHandler extends Handler {
        public MyHandler() {
            super();
        }

        public MyHandler(Looper looper) {
            super(looper);
        }

        public MyHandler(Looper looper, Callback callback) {
            super(looper, callback);
        }

        @TargetApi(Build.VERSION_CODES.M)
        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {

            myLog.info("send message at time "+ handlerThread.isAlive());

            return super.sendMessageAtTime(msg,uptimeMillis);
        }


        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void handleMessage(Message msg) {
           myLog.info("receiver message \t" + msg.hashCode());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

